package com.example.rfidwms;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private TextView textViewUserinfo;
    private Button buttonWriteTag;
    private Button buttonIn;
    private Button buttonOut;
    private Button buttonCheck;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    Toast.makeText(MainActivity.this, "获取物资编码成功", Toast.LENGTH_SHORT).show();
//                    Toast.makeText(MainActivity.this, String.valueOf(app.materialMap.size()), Toast.LENGTH_SHORT).show();
                    break;
                case 0:
//                    不能获取物资编码，入出盘功能取消
                    buttonIn.setVisibility(View.GONE);
                    buttonOut.setVisibility(View.GONE);
                    buttonCheck.setVisibility(View.GONE);
                    Toast.makeText(MainActivity.this, "获取物资编码失败", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    private final Application app = new Application();
    private final Map<String, JSONObject> tempMaterialMap = new HashMap<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textViewUserinfo = findViewById(R.id.textView_userinfo);
        buttonWriteTag = findViewById(R.id.button_writeTag);
        buttonIn = findViewById(R.id.button_in);
        buttonOut = findViewById(R.id.button_out);
        buttonCheck = findViewById(R.id.button_check);

        textViewUserinfo.setText("欢迎您，" + Application.user_name + "。  仓库号为：" + Application.dpt_id);


        OkHttpClient okHttpClient = new OkHttpClient();

        Request request = new Request.Builder()
                .post(RequestBody.create(null, ""))
                .url(Application.path_allMaterial)
                .build();

        Call call = okHttpClient.newCall(request);

        call.enqueue(new Callback() {
            final Message msg = Message.obtain();

            @Override
            public void onFailure(Call call, IOException e) {
                msg.what = 0;
                handler.sendMessage(msg);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();

                try {
                    JSONArray array = new JSONArray(string);
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject object = array.getJSONObject(i);
                        String epc = object.getString("epc");
                        tempMaterialMap.put(epc, object);
                    }
                    Application.materialMap.putAll(tempMaterialMap);
//                    app.material_jsonArray = new JSONArray(string);
                    msg.what = 1;
                    handler.sendMessage(msg);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void btn_click(View v) {
        switch (v.getId()) {
            case R.id.button_writeTag:
//                Toast.makeText(MainActivity.this, "这是第一个按钮", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this, WriteTagActivity.class); // 创建一个 Intent 对象
                intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(intent);
                break;
            case R.id.button_in:
//                Toast.makeText(MainActivity.this, "这是第二个按钮", Toast.LENGTH_SHORT).show();
                Intent intent2 = new Intent(MainActivity.this, InTagActivity.class); // 创建一个 Intent 对象
                intent2.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(intent2);
                break;
            case R.id.button_out:
//                Toast.makeText(MainActivity.this, "这是第三个按钮", Toast.LENGTH_SHORT).show();
                Intent intent3 = new Intent(MainActivity.this, OutTagActivity.class); // 创建一个 Intent 对象
                intent3.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(intent3);
                break;
            case R.id.button_check:
//                Toast.makeText(MainActivity.this, "这是第四个按钮", Toast.LENGTH_SHORT).show();
                Intent intent4 = new Intent(MainActivity.this, CheckTagActivity.class); // 创建一个 Intent 对象
                intent4.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(intent4);
                break;
        }
    }
}